package com.chaodroid.flyui.widget.point;

import android.content.Context;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.chaodroid.flyui.R;
import com.chaodriod.common.utils.screen.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CHEN on 2017/12/20.
 */

public class PointIndicator extends LinearLayout {

    private static final String TAG = "PointIndicator";
    private Context mContext;
    private int mItemCount = 0;
    private List<View> mItems = new ArrayList();

    public PointIndicator(Context context) {
        this(context, null);
    }

    public PointIndicator(Context context, @Nullable AttributeSet attrs) {
        //设置默认样式
        this(context, attrs, 0);
    }

    public PointIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init(attrs);
    }

    private void init(@Nullable AttributeSet attrs) {
    }

    public void setPosition(@IntRange(from = 0) int position) {
        for (int i = 0; i < mItems.size(); i++) {
            mItems.get(i).setSelected(i == position);
        }
    }

    public void setAdapter(PointIndicator.Adapter adapter) {
        if (adapter != null) {
            mItemCount = adapter.getItemCount();
            if (mItemCount > 0) {
                int itemMagin = adapter.getItemMagin() / 2;
                if (itemMagin < 1) {
                    itemMagin = ScreenUtils.dp2px(mContext, 5f);
                }
                for (int i = 0; i < mItemCount; i++) {
                    View item = adapter.bindItemView(i);
                    //如果 Item 为空，使用默认样式
                    if (item == null) {
                        item = new View(mContext);
                        LayoutParams layoutParams = new LayoutParams(ScreenUtils.dp2px(mContext, 8f), ScreenUtils.dp2px(mContext, 8f));
                        layoutParams.leftMargin = itemMagin;
                        layoutParams.rightMargin = itemMagin;
//                        StateListDrawable drawable = new StateListDrawable();
//                        drawable.addState(View.EMPTY_STATE_SET,ContextCompat.getDrawable(mContext,id));
                        item.setBackground(ContextCompat.getDrawable(mContext, R.drawable.point_bg_selector));
                        item.setLayoutParams(layoutParams);
                    } else {
                        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                        layoutParams.leftMargin = itemMagin;
                        layoutParams.rightMargin = itemMagin;
                        item.setLayoutParams(layoutParams);
                    }
                    addView(item);
                    mItems.add(item);
                }
                requestLayout();
            }
        }
    }

    public abstract static class Adapter {

        public abstract int getItemCount();

        /**
         *
         * @return 返回 0 使用默认设置
         */
        public abstract int getItemMagin();

        /**
         *
         * @param position
         * @return 返回空使用默认样式
         */
        public abstract View bindItemView(int position);
    }

}